/*
@ auther:ldz
@ time:2018-06-29
*/
//服务器地址
var _httppath = "http://218.246.21.190/";
var token = "";
//输入框失去焦点进行数据检索
var searchData = function () {
	//获取输入框的内容
	var _inputVal =  $("#searchInput").val().trim();
	//获取Searchitem
	var _searchitem = $(".selectTop").data("key");
	//获取Groupitem
	var _groupitem = $(".selctListTab").data("key");

	if(_searchitem == "author") {
		_groupitem = "year";
	}

	if(_inputVal == "") _inputVal = "大数据";
	//左侧搜索区域的文字变换
	$(".Js_search_content").text(_inputVal);
	getSearchData(_inputVal,_searchitem,_groupitem);		
	
}

//点击进入详情页
var getDetailData = function(_name){
		//获取输入框的内容
	var _inputVal =  $("#searchInput").val().trim();
	//获取Searchitem
	var _searchitem = $(".selectTop").data("key");
	//获取Groupitem
	var _groupitem = $(".selctListTab").data("key");	
	if(_inputVal == "") _inputVal = "大数据";
     $.ajax({
         type: "GET",
		 url: _httppath+'api/search/booklist/'+_inputVal+'?searchitem='+_searchitem+'&groupitem='+_groupitem+'&groupitem_value='+_name+"&token="+token+"&userid=1",
         data: {},
         dataType: "json",
         async:false,
         success: function(response){
 			if (response.status) {
 				console.log("listlistdata----------"+JSON.stringify(response));

 				$(".Js_search_box").hide();
 				$(".Js_list_box").show();
 				if ($(".selectTop").data("key") == "title") {
 				 	$(".Js_show_year").hide();	
 				} else if ($(".selectTop").data("key") == "author"){
 					$(".Js_show_year").show();
 				}
 				

 				var listData = response.data.items;
 				let temObj = $('.showListTemplate').find('li');
				let _ulHtml = $('<ul class="swiper-slide"></ul>');
 				for(let i = 0 ; i < listData.length; i ++) {
 					if (i%4 == 0) {
 						_ulHtml = $('<ul class="swiper-slide"></ul>');
 					}
 					$(temObj).find(".Js_list_title").text(listData[i].title);
 					$(temObj).find(".Js_list_autor").text("作者："+listData[i].author);
 					$(temObj).find(".Js_list_content").text(listData[i].summary);
 					let recommendationObj = $(temObj).find(".recommendation_box").find("li");
 					let recommendationNum = parseInt(listData[i].recommendationIndex);
 					for (let j = 0; j < recommendationNum; j++) {
 							let reObj = recommendationObj[j];
 							$(reObj).addClass("on");
 					}
 					$(temObj).find(".Js_show_url").attr("href",listData[i].url);
	 				let template = $('.showListTemplate').html(); 					
 					_ulHtml.append(template);
 					if(i%3 == 0) {
  						$(".swiper-wrapper").append(_ulHtml);						
 					}
 				}

		        var swiper = new Swiper('.swiper-container', {
		          pagination: {
		            el: '.swiper-pagination',
		            dynamicBullets: true,
		         },
		        });
 				
 			}
 		 }		
     }); 

}

//顶部选择书名、顶部选择作者
var selectTopTab = function(obj) {
	//选择作者
	if ($(obj).data("key") == "author") {
		$(".list_select_tab").hide();		
	} else if ($(obj).data("key") == "title") {//选择书名
		$(".list_select_tab").show();		
	}

	$(".selectTop").removeClass("selectTop");
	$(obj).addClass("selectTop");
}

//顶部选择书名、顶部选择作者
var selectListTab = function(obj) {
	$(".selctListTab").removeClass("selctListTab");
	$(obj).addClass("selctListTab");
}



//获取顶部书名检索
var getSearchData = function(_inputVal,_searchitem,_groupitem) {
     $.ajax({
         type: "GET",
		 url: _httppath+'api/search/group/'+_inputVal+'?groupitem='+_groupitem+'&searchitem='+_searchitem+"&token="+token+"&userid=1",
         data: {},
         dataType: "json",
         async:false,
         success: function(response){
 			if (response.status) {
 				console.log("书目data----------",response);
 				showContentPage(response.data);
 			}
 		 }		
     }); 
}

//显示豆腐块
var showContentPage = function(_array){
	//页面内容对象
	var _contentObj = $(".Js_show_content");
	//页面数量对象
	var _countObj = $(".Js_show_count");
	//页面点击对象
	var _actionObj = $(".Js_go_detal");
	for (let i = 0; i < _array.length; i++) {
		$(_contentObj[i]).text(_array[i].name);
		$(_countObj[i]).text(_array[i].count);
		$(_actionObj[i]).click(function(){
			getDetailData(_array[i].name);
		})
	}
	// _actionObj.each(function(){
	// 	$(this).click(function(_obj,_index){
	// 		alert(_index);
	// 	})
	// })


}

//模块介绍
$(function(){
	//首页数据
	var arry = [];

    //获取token
         $.ajax({
             type: "GET",
			 url: _httppath+"api/appsetting/getToken",
             data: {},
             async:false,
             dataType: "json",
             success: function(response){
     			if (response.status) {
     				//存储token
     				 token =  response.data.access_token;
     				//存储token
					// token = token;	
					//有效期倒计时
					var expires = Number(response.data.expires_in);
					console.log("response",response);	
					var setInterVal = setInterval(function(){
						expires--;
						if (expires == 0) {
							token = "";
							window.clearInterval(setInterVal);
						}
					},1000);

			     if (token != "") {
					//获取图书馆名称信息
			         $.ajax({
			             type: "GET",
						 url: _httppath+"api/appsetting/list"+"?token="+token,
			             data: {},
			             async:false,
			             dataType: "json",
			             success: function(response){
			     			if (response.status) {
			     				var	logoDataArray = response.data;
								//设置logo
								  $(".logo2").attr("src",_httppath+logoDataArray[1].settingValue);
			     			}
			             }
			         });

					//获取热门关键词				        
			         $.ajax({
			             type: "GET",
						 url: _httppath+"api/search/hotkeyword"+"?token="+token,
			             data: {},
			             async:false,
			             dataType: "json",
			             success: function(response){
			     			if (response.status) {
			     				var	lotkeywordArray = response.data;
								//获取输入框的内容
								var _inputObj =  $("#searchInput");
								//左侧显示的搜索数据
								var _leftSearchObj = $(".Js_search_content");
								//获取Searchitem
								var _searchitem = $(".selectTop");
								//获取Groupitem
								var _groupitem = $(".selctListTab");				     				
			     				var hotBox = $(".Js_hot_box");
									for (let i = 0; i < lotkeywordArray.length; i++) {
										let _a = $('<a></a>');
										let _queryString = lotkeywordArray[i].queryString;
										//点击进行检索
										_a.click(function(){
											//_queryValue
											let _queryValue = _queryString.substring(0,_queryString.indexOf("?"));
											let groupitemstr = _queryString.substring(_queryString.indexOf("?"),_queryString.indexOf("&"));
											let searchitemstr = _queryString.substr(_queryString.indexOf("&")+1);
											//_searchitemVal
											let _searchitemVal = searchitemstr.substr(searchitemstr.indexOf("=")+1);
											//groupitemVal
											let _groupitemVal = groupitemstr.substr(searchitemstr.indexOf("=")+1);
											if (_searchitemVal.indexOf("title") > -1) {
												$(".list_select_tab").show();	
												$(".selectTop").removeClass("selectTop");
												$("#selectTitle").addClass("selectTop");	
												if (_groupitemVal.indexOf("keyword") > -1) {
													$(".selctListTab").removeClass("selctListTab");
													$("#selectKeyWordList").addClass("selctListTab");
												} else if (_groupitemVal.indexOf("author") > -1) {
													$(".selctListTab").removeClass("selctListTab");
													$("#selectauthorList").addClass("selctListTab");													
												}	 										
											} else if (_searchitemVal.indexOf("author") > -1) {
													$(".list_select_tab").hide();
											}
											//搜索框中的文字
											_inputObj.val(_queryValue);
											//左侧搜索区域的文字变换
											_leftSearchObj.text(_queryValue);		

											     $.ajax({
										         type: "GET",
												 url: _httppath+'api/search/group/'+_queryString+"&userid=1"+"&token="+token,
										         data: {},
										         dataType: "json",
										         async:false,
										         success: function(response){
										 			if (response.status) {
										 				console.log("书目data----------",response);
										 				showContentPage(response.data);
										 			}
										 		 }		
										     });				
										})
										_a.text(lotkeywordArray[i].keyword);
										$(hotBox).append(_a);
									}
			     			}
			             }
			         });		

			        //默认获取主题词+书目聚合 默认检索大数据
			        getSearchData("大数据","title","keyword");

			 	   }	
  			}
	 	}
 	 });
	//上一页方法
	$(".Js_last_page").click(function(){
		$(".Js_list_box").hide();	
		$(".Js_search_box").show();
		$(".swiper-wrapper").html("");
	})
	//主页点击方法
	$(".Js_home").click(function(){
		window.location.href = "index.html";
	})


	//唤起andorid语音方法
	$(".Js_voice_btn").click(function(){
		//获取Searchitem
		var _searchitem = $(".selectTop").data("key");
		//获取Groupitem
		var _groupitem = $(".selctListTab").data("key");		
		window.location.href = "reeman://m.libmanai.com/speech/SpeechServerActivity?searchitem="+_searchitem+"&groupitem="+_groupitem;


		})



})